<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/11/13 0013
 * Time: 16:24
 */

namespace app\admin\controller;
use app\index\model\Question;
use think\Controller;
use think\Db;

class Ques extends Common
{
	/**
	 * 帖子管理主页
	 * @return mixed
	 */
	public function index()
	{
		/**
		 * 排序
		 */
		$order = [];
		input('view_num') == 'desc' && $order['view_num'] = 'desc';
		input('view_num') == 'ase' && $order['view_num'] = '';
		input('reply_num') == 'desc' && $order['reply_num'] = 'desc';
		input('reply_num') == 'ase' && $order['reply_num'] = '';
		input('kiss') == 'desc' && $order['kiss'] = 'desc';
		input('kiss') == 'ase' && $order['kiss'] = '';
		input('ID') == 'desc' && $order['question_id'] = 'desc';
		input('ID') == 'ase' && $order['question_id'] = '';
		/**
		 * 搜索
		 */
		$where = [];
		input('status') == 1 && $where['status'] = 1;
		input('status') == 2 && $where['status'] = 0;
		input('is_top') == 1 && $where['is_top'] = 1;
		input('is_top') == 2 && $where['is_top'] = 0;
		input('is_good') == 1 && $where['is_good'] = 1;
		input('is_good') == 2 && $where['is_good'] = 0;
		/**
		 * 根据传过来的数据进行查找
		 */
		$question = Db::name('question')
			->field('t1.*,t2.nickname,t3.cate_name')
			->alias('t1')
			->join('user t2' , 't1.user_id = t2.user_id ')
			->join('cate t3' , 't1.cate_id = t3.cate_id')
			->where($where)
			->where('t1.delete_time',null)
			->order($order)
			->paginate(10);
		$questions = [];
		/**
		 * 转时间戳
		 */
		foreach ( $question as $que ) {
			$que['create_time'] = date('Y-m-d H:i:s' , $que['create_time']);
			$questions[] = $que;
		}
		$this->assign('ques' , $questions);
		$this->assign('we' , $question);
		return $this->fetch();
	}


	/**
	 * 软删除问题方法
	 */
	public function del()
	{
		$questionId = input('post.');
		$res = Question::destroy($questionId['question_id']);
		if ($res){
			$this->success('已删除');
		}else{
			$this->error('删除失败，请稍后再试');
		}
	}


}